package com.imooc.controller;

import com.google.gson.Gson;
import com.imooc.api.intercept.JWTCurrentUserInterceptor;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.grace.result.IMOOCJSONResult;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.pojo.Stu;
import com.imooc.pojo.Users;
import com.imooc.service.StuService;
import com.imooc.utils.MyInfo;
import com.imooc.utils.SMSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import static com.imooc.base.BaseInfoProperties.APP_USER_JSON;

/**
 * @version 1.0
 * @Author Mr.Yang
 * @Date 2024-08-09 16:11
 * @question: 测试类
 */

@RestController
@RequestMapping("u")
@Slf4j
public class HelloController {

    @Autowired
    private StuService stuService;

    @Autowired
    private SMSUtils smsUtils;

    @GetMapping("sms")
    public Object sms() throws Exception {

        smsUtils.sendSMS(MyInfo.getMobile(),"662211");

        return "Send SMS OK~~";
    }

    @GetMapping("stu")
    public Object stu() {

        Stu stu = new Stu();
//        stu.setId("1001");
        stu.setAge(18);
        stu.setName("慕课网 www.imooc.com");

        stuService.save(stu);

        return "OK";
    }

    @GetMapping("hello")
    public Object hello(HttpServletRequest request) {

        String userJson = request.getHeader(APP_USER_JSON);
        Users jwtUser = new Gson().fromJson(userJson, Users.class);
        log.info(jwtUser.toString());

        Users users = JWTCurrentUserInterceptor.currentUser.get();
        log.info("JWTCurrentUserInterceptor: " + users.toString());
        //Stu stu = new Stu(1001, "imooc", 18);

//        System.out.println(stu.toString());
        //log.info("info：" + stu.toString());
        //log.debug("debug：" + stu.toString());
        //log.warn("warn：" + stu.toString());
        //log.error("error：{}", stu.toString());

        return "Hello UserService~~~";
    }

    @GetMapping("hello2")
    public IMOOCJSONResult hello2() {
        Stu stu = new Stu(1001, "imooc", 18);

        return IMOOCJSONResult.ok(stu);
//        return IMOOCJSONResult.ok("添加成功！");
//        return IMOOCJSONResult.errorMsg("修改出错，请联系管理员");
//        return IMOOCJSONResult.errorMap(map)
//        return IMOOCJSONResult.errorUserTicket("用户会话校验失败！");
    }

    @GetMapping("hello3")
    public GraceJSONResult hello3() {
        Stu stu = new Stu(1001, "imooc", 18);

//        return GraceJSONResult.ok(stu);
//        return IMOOCJSONResult.ok("添加成功！");
//        return IMOOCJSONResult.errorMsg("修改出错，请联系管理员");
//        return IMOOCJSONResult.errorMap(map)
        return GraceJSONResult.errorCustom(ResponseStatusEnum.SYSTEM_IO);
    }
}
